package com.ym.manager.admin.product;

import com.ym.manager.admin.goods.model.Goods;
import com.ym.manager.admin.product.model.Promote;
import com.ym.utils.Common;
import com.ym.utils.DateUtil;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/promote")
public class PromoteController {

    @Autowired
    private PromoteService promoteService;

    /**
     * 页面：产品集主页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "/promote/index";
    }

    /**
     * 功能：产品集列表数据
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public String getList(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10")int limit){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<Promote> pages = promoteService.getList(page,limit);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsFiltered","");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 页面：新增产品集
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "/promote/add";
    }

    /**
     * 页面：选择产品集产品
     * @return
     */
    @RequestMapping("/addPromote")
    public String addPromote(Model model,@RequestParam(defaultValue = "") String id){
        if(Common.isNotEmpty(id)){
            model.addAttribute("ids",Arrays.asList(id.split(",")));
        }
        return "promote/addPromote";
    }

    /**
     * 功能：获取产品集产品列表
     * @param name 产品名称
     * @param categoryId 类目ID
     * @return
     */
    @RequestMapping("/getPromote")
    @ResponseBody
    public String getPromote(@RequestParam(defaultValue = "")String name,
                             @RequestParam(defaultValue = "0")int categoryId,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "20")int limit){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<Goods> pages = promoteService.getPromote(page,limit,name,categoryId);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsFiltered","");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：新增产品集
     * @param name 产品集名称
     * @param placeId 位置ID
     * @param start 开始时间
     * @param over 结束时间
     * @param pic logo
     * @param promoteGoods 关联项目
     * @param status 状态 1否 2是
     * @param remark 备注
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(@RequestParam(defaultValue = "0") int id,
                         @RequestParam(defaultValue = "")String name,
                         @RequestParam(defaultValue = "1")int placeId,
                         @RequestParam(defaultValue = "")String start,
                         @RequestParam(defaultValue = "")String over,
                         @RequestParam(defaultValue = "")String pic,
                         @RequestParam(defaultValue = "")String promoteGoods,
                         @RequestParam(defaultValue = "1")int status,
                         @RequestParam(defaultValue = "")String remark){
        Map<String,Object> result = ResultUtil.result();
        Map<String,Object> params = new HashMap<>();
        params.put("name",name);
        params.put("placeId",placeId);
        params.put("start",start);
        params.put("over",over);
        params.put("pic",pic);
        params.put("promoteGoods",promoteGoods);
        params.put("status",status);
        params.put("create_time", DateUtil.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        if(Common.isNotEmpty(remark)){
            params.put("remark",remark);
        }
        try {
            int i;
            if(id>0){
                params.put("id",id);
                i = promoteService.update(id,params);
                result.put("code",i>1?0:-1);
                result.put("msg",i>1?"修改成功":i==1?"修改失败，产品关联错误":"修改失败，请联系管理员");
            }else{
                i = promoteService.insert(params);
                result.put("code",i>1?0:-1);
                result.put("msg",i>1?"新增成功":i==1?"新增失败，产品关联错误":"新增失败，请联系管理员");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 页面：修改产品集
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Model model,int id){
        model.addAttribute("promote",promoteService.detail(id));
        return "/promote/edit";
    }

    /**
     * 功能：修改产品集上下线状态
     * @param id 产品集记录id
     * @param status 状态 1否 2是
     * @return
     */
    @RequestMapping("/shelf")
    @ResponseBody
    public String shelf(int id,
                        int status){
        Map<String,Object> result = ResultUtil.result();
        try {
            int i = promoteService.shelf(id,status);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"操作成功":"操作失败");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：删除产品集
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        Map<String,Object> result = ResultUtil.result();
        try {
            Promote p = promoteService.detail(id);
            if(p.getPlaceId()<7){
                result.put("code",-1);
                result.put("msg","该产品集不能删除");
                return ResultUtil.toJSON(result);
            }
            int i = promoteService.delete(id);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"删除成功":"删除失败");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
}
